﻿using AutoMapper;
using MediatR;
using MS.BasicData.Domain;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;
using System.Security;
using System.Security.Claims;

namespace MS.BasicData.Read.API.Applications.Permissionss
{
    public class GetTreePermissionListRequestHandler : IRequestHandler<GetTreePermissionListRequestModel, ApiRestult>
    {
        private readonly IRolePermissionBaseRepository _rolePermissionRepository;

        private readonly IUserRoleBaseRepository _userRoleRepository;

        private readonly IPermisBaseBaseRepository _permissionRepository;

        private readonly IHttpContextAccessor _httpContextAccessor;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rolePermissionRepository"></param>
        /// <param name="userRoleRepository"></param>
        /// <param name="permissionRepository"></param>
        /// <param name="httpContextAccessor"></param>
        /// <param name="mapper"></param>
        public GetTreePermissionListRequestHandler(IRolePermissionBaseRepository rolePermissionRepository, IUserRoleBaseRepository userRoleRepository, IPermisBaseBaseRepository permissionRepository, IHttpContextAccessor httpContextAccessor)
        {
            _rolePermissionRepository = rolePermissionRepository;
            _userRoleRepository = userRoleRepository;
            _permissionRepository = permissionRepository;
            _httpContextAccessor = httpContextAccessor;
        }

        public async Task<ApiRestult> Handle(GetTreePermissionListRequestModel request, CancellationToken cancellationToken)
        {

            // 从权限仓库获取所有权限数据
            var permissions = await _permissionRepository.GetAllAsync();

            // 将权限数据转换为树状结构
            var tree = BuildPermissionTree(permissions);

            // 返回ApiRestult
            return new ApiRestult
            {
                isSuc = true,
                Data = tree
            };
        }

        private IList<GetTreePermissionListResponseModel> BuildPermissionTree(IList<Permissions> permissions)
        {
            // 使用字典构建权限ID与权限对象的映射
            var permissionDict = permissions.ToDictionary(p => p.PermissionId);

            // 创建一个列表来保存树的根节点
            var tree = new List<GetTreePermissionListResponseModel>();

            foreach (var permission in permissions)
            {
                // 创建响应模型
                var responseModel = new GetTreePermissionListResponseModel
                {
                    PermissionId = permission.PermissionId,
                    PermissionsName = permission.PermissionsName,
                    Path = permission.Path,
                    Name = permission.Name,
                    Component = permission.Component,
                    Children = new List<GetTreePermissionListResponseModel>()
                };

                // 添加到根节点
                tree.Add(responseModel);
            }
            return tree;
        }

    }
    public class GetTreePermissionListRequestModel : IRequest<ApiRestult>
    {

    }
    


    public class GetTreePermissionListResponseModel
    {
        public int PermissionId { get; set; }
        public string PermissionsName { get; set; } = string.Empty;
        public string Path { get; set; } = string.Empty;
        public string Name { get; set; } = string.Empty;
        public string Component { get; set; } = string.Empty;
        public IList<GetTreePermissionListResponseModel> Children { get; set; }
    }
}
